/*
ehs - 2010-03-25
iPhone specific JavaScript functions
*/

/*
untestable in Chrome
- swipe pads -> pad select for rename
*/

// is this even used?
function removeAddressBar()
	{ setTimeout(function() { window.scrollTo(0, 0); }, 100); }


setCookieOptions();


var jqt = $.jQTouch();
var flickr = Flickr();


$(function() {
	setTimeout(function() {
		flickr.checkAuth(authCallback);
	}, 1000);

	$("#validAuth").bind("pageAnimationStart", preShowValidAuth);

	$("#setSelectDiv button").click(onGoClicked);

	$("#picture").tap(function() {
		var cssObj = {
			width: this.width == 180 ? 240 : 180,
			height: this.height == 180 ? 240 : 180
		}
		$(this).css(cssObj);
	});

	// in order for swipe to work correctly, the top-most elements (li) also need "touch" class
	$("#main .pads").addClass("touch").swipe(mainPadsSwiped);
	$("#main .pads li").addClass("touch").click(mainPadClicked);

	$("#prNext").click(prNextClicked);
	$("#prDone").click(prDoneClicked);
	$("#padRenamePage .pads").click(prPadsClicked);

	$("#psClear").click(psClearClicked);
	$("#psDone").click(psDoneClicked);
	$("#padSelectPage .pads li").click(psPadClicked);
});


function setCookieOptions()
{
	var monthFromToday = new Date();
	monthFromToday.setDate(monthFromToday.getDate() + 30);
	$.cookies.setOptions({
		domain: ".cmepw.org",
		path: "/WebTag",
		expiresAt: monthFromToday
	});
}


function getParameterByName(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if (results == null)
		return "";
	else
		return results[1];
}


function authCallback(isAuthorized)
{
	if (isAuthorized) {
		$(".userNameSpan").html(flickr.getUserName());
		jqt.goTo("#validAuth", "slide");
	}
	else
		jqt.goTo("#noAuth", "slide");
}


function preShowValidAuth(event, info) {
	if (info.direction != "in")
		return;

	$("#loadingSetsP").css("display", "");
	$("#setSelectDiv").css("display", "none");
	$("#loadingPhotosP").css("display", "none");

	var setSelect = $("#setSelectDiv select")[0];
	while (setSelect.length > 0)
		setSelect.remove(0);
	setSelect.disabled = true;
	$("#setSelectDiv button")[0].disabled = true;

	flickr.getSets(setsCallback);
}


function setsCallback(setMap)
{
	var setSelect = $("#setSelectDiv select")[0];
	for (setName in setMap) {
		var newOption = document.createElement("option");
		newOption.text = setName;
		newOption.value = setMap[setName];
		setSelect.add(newOption);
	}
	setSelect.disabled = false;
	if (setSelect.length > 0)
		$("#setSelectDiv button")[0].disabled = false;

	$("#loadingSetsP").css("display", "none");
	$("#setSelectDiv").css("display", "");
}


function onGoClicked() {
	var setSelect = $("#setSelectDiv select")[0];
	if (setSelect.selectedIndex < 0)
		return;

	$("#setSelectDiv").children().each(function() { this.disabled = true; });

	$("#loadingPhotosP").css("display", "");
	flickr.selectSet(setSelect[setSelect.selectedIndex].value, function() {
		jqt.goTo("#main", "slide");
	});
}


function mainPadsSwiped()
{
	jqt.goTo("#padSelectPage", "slide");
	return false;
}


// fixme: this can get double-tapped causing jQTouch console errors
function mainPadClicked(event)
{
	var clickedPad = $(this);
	if (clickedPad.text() == "")
		showPadRenamePage(clickedPad.index());
	return false;
}


function showPadRenamePage(index)
{
	var pads = $("#padRenamePage .pads li");
	pads.removeClass("selected");

	var selectedPad = pads.eq(index);
	selectedPad.addClass("selected");
	var previousTag = selectedPad.text();

	var newTagInput = $("#newTag");
	newTagInput.val(previousTag);
	$("#padRenamePage span").text(previousTag);
	
	// fixme: add a (x) button to clear out the text
	if (!$("#padRenamePage").hasClass("current"))
		jqt.goTo("#padRenamePage", "slide");
	newTagInput.focus(); // fixme: this doesn't work on iPhone in a setTimeout or pageAnimationEnd AT ALL!  bug in iSafari?
}


// fixme: <return> should advance to next tag, methinks
function prNextClicked()
{
	var selectedPad = $("#padRenamePage .pads li.selected");
	if (selectedPad.length > 0) {
		setTag(selectedPad.index(), $("#newTag").val());
		var currentSelectedIndex = selectedPad.index();
		showPadRenamePage(++currentSelectedIndex % 9);
	} // error otherwise?
	return false;
}


function prDoneClicked()
{
	var selectedPad = $("#padRenamePage .pads li.selected");
	if (selectedPad.length > 0) // error otherwise?
		setTag(selectedPad.index(), $("#newTag").val());
	jqt.goTo("#main", "slide"); // fixme: would be nice if rename pages slid down on entrance, then back up to return
	return false;
}


function prPadsClicked()
{
	jqt.goTo("#padSelectPage", "slide");
	return false;
}


function psClearClicked()
{
	if (confirm("Are you sure you want to clear all tag pads?"))
		$(".pads li").text("");
	return false;
}


function psDoneClicked()
{
	jqt.goTo("#main", "slide"); // fixme: would be nice if rename pages slid down on entrance, then back up to return
	return false;
}


function psPadClicked()
{
	showPadRenamePage($(this).index());
	return false; // fixme: should we be using "event.preventDefault" (or whatever) instead of "return false"?
}


function setTag(index, newTag)
{
	// have to use :nth-child because :eq only selects a single element
	$(".pads li:nth-child(" + (index + 1) + ")").text(newTag); // :nth-child is 1-based
}



